package com.xc.ksxt.controller;

import com.alibaba.fastjson.JSON;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.Grade;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.entity.GradeEntity;
import com.xc.ksxt.service.GradeService;
import com.xc.ksxt.vo.GradeVO;
import com.xc.sys.entity.SysUserEntity;
import com.xc.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grade")
@CrossOrigin
public class GradeController {

    @Autowired
    private GradeService GradeService;
    private Grade grade = Grade.GRADE.as("grade");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<GradeVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<GradeVO> ltr = new LayuiTableResponse<>();
        Condition cond = grade.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(grade.NAME.like("%"+name+"%"));
        }
        Long count = GradeService.count(cond);
        ltr.setCount(count);
        PageInfo<GradeEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> r = GradeService.select(cond, pageInfo,grade.NAME.desc());
        List<GradeVO> list = r.into(GradeVO.class);
        for (GradeVO v : list) {
        }
        ltr.setData(list);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public GradeEntity getById(Long id) {
        GradeEntity entity = GradeService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<GradeEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<GradeEntity> res = new ResponseEntity<>();
        GradeEntity gradeEntity = Helper.map2Bean(new GradeEntity(), params);
        GradeService.save(gradeEntity);
        return res;
    }
    @RequestMapping({"/updateOrderno"})
    public ResponseEntity<LibQuestionEntity> updateOrderno(@RequestBody HashMap<String,String> params) {
        Long id = Long.valueOf(params.get("id"));
        Integer orderno = Integer.valueOf(params.get("orderno"));
        ResponseEntity<LibQuestionEntity> res = new ResponseEntity<>();
        GradeEntity lq = GradeService.getEntityById(id);
        lq.setOrderno(orderno);
        GradeService.update(lq);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<GradeEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Long[] idarr = Helper.str2longArr(ids);
        LayuiTableResponse<GradeEntity> ltr = new LayuiTableResponse<>();
        for (Long id : idarr) {
            GradeEntity m = GradeService.getEntityById(id);
            m.setDelFlag(-1);
            GradeService.update(m);
        }
        return ltr;
    }

}
